/**
 * Copyright 2015 lerry
 * 
 * All right reserved.
 * 
 * Apr 2, 2015
 */
package com.bac.bacplatform;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.sql.PooledConnection;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.GasolineJournalInfo;
import com.bac.utils.Config;
import com.bac.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author lerry
 *
 */
public class CouponDetailActivity extends BaseActivity {
	
	private ListView mListView;
	
	private ArrayList<GasolineJournalInfo> mDataList;
	
	private String mIdString;
	
	private DeatailAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_coupon_detail);
		
		mIdString = getIntent().getStringExtra("id");
		
		mDataList = new ArrayList<GasolineJournalInfo>();
		mAdapter = new DeatailAdapter(this, mDataList);
		getGasonlineDetail();
	}

	@Override
	public void init() {
		
		findViewById(R.id.title_left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {		
				finishActivityOut();
			}
		});
		
		mListView = (ListView)findViewById(R.id.list_detail);
		mListView.setAdapter(mAdapter);
	}

	
	class DeatailAdapter extends BaseAdapter{
		private ArrayList<GasolineJournalInfo> mArrayList;
		private Context mContext;

		public DeatailAdapter(Context context, ArrayList<GasolineJournalInfo> list){
			this.mContext = context;
			this.mArrayList = list;
		}
		@Override
		public int getCount() {
			return this.mArrayList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("SimpleDateFormat")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			GasolineJournalInfo gasolineJournalInfo = this.mArrayList.get(position);
			final ViewHolder viewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.coupon_detail_item, null);
				viewHolder = new ViewHolder();
				viewHolder.headImg = (ImageView)convertView.findViewById(R.id.image_first);
				viewHolder.benjinImg = (ImageView)convertView.findViewById(R.id.img_benjin);
				viewHolder.songImg = (ImageView)convertView.findViewById(R.id.img_song);
				viewHolder.timeTV = (TextView)convertView.findViewById(R.id.tv_time);
				viewHolder.benjinTV = (TextView)convertView.findViewById(R.id.tv_benjin);
				viewHolder.songTV = (TextView)convertView.findViewById(R.id.tv_song);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			
			String date = gasolineJournalInfo.getDate();
			BigDecimal capital = gasolineJournalInfo.getCapital();
			BigDecimal preferential = gasolineJournalInfo.getPreferential();
			BigDecimal returnedCapital = gasolineJournalInfo.getReturnedCapital();
			BigDecimal returnedPreferential = gasolineJournalInfo.getReturnedPreferential();
			
			viewHolder.benjinTV.setText(capital.toString()+"(元)");
			viewHolder.songTV.setText(preferential.toString()+"(元)");
			
			Date now = new Date();
			Date sDate = null;
			SimpleDateFormat sf = new SimpleDateFormat("yyyy.MM");
			String nowString = sf.format(now);
			try {
				sDate = sf.parse(date);
				now = sf.parse(nowString);
				if (now.getTime() - sDate.getTime() == 0) {
					viewHolder.headImg.setImageResource(R.drawable.profile_bg_bigdoint_finished);
					viewHolder.timeTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
				}else if(now.getTime() - sDate.getTime() < 0){
					viewHolder.headImg.setImageResource(R.drawable.profile_bg_smalldoint_not);
					viewHolder.timeTV.setTextColor(getResources().getColor(R.color.login_black));
				}else{
					viewHolder.headImg.setImageResource(R.drawable.profile_bg_smalldoint_finished);
					viewHolder.timeTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
				}
				
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if (capital.compareTo(BigDecimal.ZERO) == 0) {
				if ((now.getTime() - sDate.getTime() >= 0)) {
					viewHolder.benjinTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
					 viewHolder.benjinImg.setImageResource(R.drawable.profile_icon_ben_finished);
				}else {
					viewHolder.benjinTV.setTextColor(getResources().getColor(R.color.login_black));
					viewHolder.benjinImg.setImageResource(R.drawable.profile_icon_ben_not);
				}
			}else{
				 if (capital.compareTo(returnedCapital) == 0) {
					 viewHolder.benjinTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
					 viewHolder.benjinImg.setImageResource(R.drawable.profile_icon_ben_finished);
				}else{
					viewHolder.benjinTV.setTextColor(getResources().getColor(R.color.login_black));
					viewHolder.benjinImg.setImageResource(R.drawable.profile_icon_ben_not);
				}
			}
			if (preferential.compareTo(BigDecimal.ZERO) == 0) {
				if ((now.getTime() - sDate.getTime() >= 0)) {
					viewHolder.songTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
					viewHolder.songImg.setImageResource(R.drawable.profile_icon_song_finished);
				}else {
					viewHolder.songTV.setTextColor(getResources().getColor(R.color.login_black));
					viewHolder.songImg.setImageResource(R.drawable.profile_icon_song_not);
				}
			}else{
				if (preferential.compareTo(returnedPreferential) == 0) {
					viewHolder.songTV.setTextColor(getResources().getColor(R.color.recharge_highlight));
					viewHolder.songImg.setImageResource(R.drawable.profile_icon_song_finished);
				}else{
					viewHolder.songTV.setTextColor(getResources().getColor(R.color.login_black));
					viewHolder.songImg.setImageResource(R.drawable.profile_icon_song_not);
				}
			}
			
			
			
			
			viewHolder.timeTV.setText(date);
			
			return convertView;
		}
		
		class ViewHolder{
			ImageView headImg, benjinImg, songImg;
			TextView  timeTV, benjinTV, songTV;
		}
	}
	
	public void getGasonlineDetail(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i(getClass().getName()+"+++++++++", response);
				
				Type listType = new TypeToken<ArrayList<GasolineJournalInfo>>(){}.getType();
				Gson gson = new Gson();
				ArrayList<GasolineJournalInfo> temp = gson.fromJson(response, listType);
				mDataList.clear();
				mDataList.addAll(temp);
				Log.i("list size", ""+mDataList.size());
				mAdapter.notifyDataSetChanged();
			}
		};
		
		showLoadingProgress();
		String path = Config.URL_COUPON_STATISTIC_DETAIL;
		Map<String, String> map = new HashMap<String, String>();
		map.put("accountId", mIdString);
		Util.httpRequestToGetWithParams(path, sL, this, map);
	}
}
